<!DOCTYPE html>
<html lang="en">

<head>
    <title>新增记录</title>
    <link rel="stylesheet" type="text/css" th:href="@{../static/css/add.css}"/>
</head>
<body>
    <button id="back"><a th:href="@{/toIndex}">返回</a></button>
    <div id="addText">
        <h2>新增记录</h2>
        <form method="post" action="/add/newRecord" name="editForm">
            添加者：<input type="text" name="adder" th:value="${session.name}" readonly><br/>
            金&emsp;额：<input type="text" name="money" required><br/>
            类&emsp;型：<input type="text" name="type" required><br/>
            日&emsp;期：<input type="text" name="date" required><br/>
            描&emsp;述：<input type="text" name="describes" required><br/>
            <input type="button" value="增加" class="addBtn" onclick="sub()">
        </form>
    </div>
</body>

<script>
    function sub(){
        //验证标记
        var flag = 0;
        console.log();

        var money_res = document.getElementsByName("money")[0].value.match(/\d+.?\d*/g);
        if (money_res.length===1 && money_res[0]!="") flag++;
        else alert("金额数值有误！")

        var type = document.getElementsByName("type")[0].value;
        if (type==="0" || type==="1") flag++;
        else alert("类型有误！");

        var date = document.getElementsByName("date")[0].value.match(/\d{4}\-\d{2}\-\d{2}/g);
        if (date!=null){
            if (date.length === 1 && date[0] != "") flag++;
            else alert("日期格式错误！");
        }else alert("日期格式错误！");

        //数据都对就submit
        if (flag===3) document.editForm.submit();
    }
</script>

</html>